package view;
import commen.check;
import dao.*;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/detail1.html")
public class detail_Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置格式
        resp.setContentType("text/html;charset=utf-8");
        //检测是否登录
        User user= check.checkSession(req);
        if(user==null){
            resp.sendRedirect("login.html");
            return;
        }
        //通过blogId获取到blog
        String blogId=req.getParameter("blogId");
        if(blogId==null||blogId.equals("")){
            String html="没有找到blog";
            resp.getWriter().write(html);
            return;
        }
        blog blog=blogdao.getBlog(Integer.parseInt(blogId));
        User author=UserDao.selectById(blog.getUseId());
        int count=blogdao.count(blog.getUseId());
        //通过模板引擎进行构造
        TemplateEngine engine= (TemplateEngine) this.getServletContext().getAttribute("engine");
        WebContext context=new WebContext(req,resp,this.getServletContext());
        context.setVariable("count",count);
        context.setVariable("blog",blog);
        context.setVariable("user",author);
        context.setVariable("showDelete",user.getUserId()==blog.getUseId());
        String html= engine.process("detail1",context);
        resp.getWriter().write(html);
    }
}
